
/**
 * 单选按钮加载器
 */
+ function($, app) {
  var template = '{{#each opts}}<label class="radio-inline"><input type="radio" name="{{name}}" data-text-val="{{text}}" value="{{val}}"{{#if checked}} checked="checked"{{/if}} /> {{text}} </label>{{/each}}';

  var render = function(elem, options) {
    //
    //获取数据源
    //
    var store = elem.data('radioloaderStore');
    if (store) {
      store = options[store];
    }

    if (store) {
      var
        //值属性
        vkey = elem.data('radioloaderVkey') || 'val',
        //文本属性
        tkey = elem.data('radioloaderTkey') || vkey,
        //当前选中值
        val = elem.data('radioloaderVal') || store[0][vkey],
        //构造列表项数组
        opts = $.map(store, function(i) {
          return { name: elem.data('radioloaderName'), val: i[vkey], text: i[tkey], checked: (i[vkey] == val) };
        });

      //填充单选按钮
      elem.append(Handlebars.compile(template)({ opts: opts }));
    }
  };

  $.fn.radioloader = function(options) {
    return this.each(function() {
      render($(this), options);
    });
  };

  //
  //单选按钮label文本隐藏域自动赋值
  //
  $('[data-text-input].radio-list').change(function() {
    $($(this).data('textInput')).val($(this).find("input:checked").data('textVal'));
  });

} (window.jQuery, window.app);

/**
 * 复选框加载器
 */
+ function($, app) {
  var template = '{{#each opts}}<label class="checkbox-inline"><input type="checkbox" name="{{name}}" data-text-val="{{text}}" value="{{val}}"{{#if checked}} checked="checked"{{/if}} /> {{text}} </label>{{/each}}';

  var render = function(elem, options) {
    //
    //获取数据源
    //
    var store = elem.data('checkboxloaderStore');
    if (store) {
      store = options[store];
    }

    if (store) {
      var
        //值属性
        vkey = elem.data('checkboxloaderVkey') || 'val',
        //文本属性
        tkey = elem.data('checkboxloaderTkey') || vkey,
        //构造列表项数组
        opts = $.map(store, function(i) {
          return { name: elem.data('checkboxloaderName'), val: i[vkey], text: i[tkey], checked: (i[vkey] == elem.data('checkboxloaderVal')) };
        });

      //填充复选框
      elem.append(Handlebars.compile(template)({ opts: opts }));
    }
  };

  $.fn.checkboxloader = function(options) {
    return this.each(function() {
      render($(this), options);
    });
  };

  //
  //复选框label文本隐藏域自动赋值
  //
  $('[data-text-input].checkbox-list').change(function() {
    var texts = [];
    $(this).find("input:checked").each(function(){
      texts.push($(this).data('textVal'));
    });
    $($(this).data('textInput')).val(texts);
  });

} (window.jQuery, window.app);

/**
 * 下拉列表加载器
 */
+ function($, app) {
  var template = '{{#each opts}}<option data-text-val="{{text}}" data-cascade-store="{{cascade}}" value="{{val}}"{{#if selected}} selected="selected"{{/if}}>{{display}}</option>{{/each}}';

  var render = function(elem, options) {
    //
    //获取数据源
    //
    var store = elem.data('selectloaderStore');
    if (store) {
      store = options[store];
    }

    if (store) {
      var
        //值属性
        vkey = elem.data('selectloaderVkey') || 'val',
        //文本属性
        tkey = elem.data('selectloaderTkey') || vkey,
        //文本显示属性
        dkey = elem.data('selectloaderDkey') || tkey,
        //级联属性
        ckey = elem.data('selectloaderCkey'),
        //构造列表项数组
        opts = $.map(store, function(i) {
          return { val: i[vkey], text: i[tkey], display: i[dkey], cascade:i[ckey], selected: (i[vkey] == elem.data('selectloaderVal')) };
        });

      //填充下拉框
      elem.append(Handlebars.compile(template)({ opts: opts }));
    }
  };

  $.fn.selectloader = function(options) {
    return this.each(function() {
      render($(this), options);
    });
  };

  //
  //下拉框关联文本隐藏域自动赋值
  //
  $('select[data-text-input]').change(function() {
    var texts = [];
    $(this).find("option:selected").each(function(){
      texts.push($(this).data('textVal'));
    });
    $($(this).data('textInput')).val(texts);
  });

  //
  //文本级联项加载数据
  //
  $('select[data-cascade-select]').change(function() {
    var cascade = $($(this).data('cascadeSelect'));
    cascade.empty().append('<option value="">请选择...</option>');
    var cascadeKstore = cascade.data('selectloaderStore');
    var cascadeStore = $(this).find("option:selected").data('cascadeStore');
    $($(this).data('cascadeSelect')).selectloader({
      cascadeKstore:cascadeStore
    });
  });

} (window.jQuery, window.app);

/**
 * 下拉树控件加载器
 */
+ function($, app) {
  var container =
    '<div class="btn-group jstreeselect form-control">' +
    '  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">' +
    '    <span class="filter-option pull-left">&nbsp;</span>' +
    '    <span class="caret"></span>' +
    '  </button>' +
    '  <div class="dropdown-menu hold-on-click"><div></div></div>' +
    '</div>';

  var render = function(elem, options) {
    //
    //获取数据源
    //
    var store = elem.data('jstreeselectStore');
    if (store) {
      store = options[store];
    }

    if (store) {
      var
        //dropdown容器
        $container = $(container).insertAfter(elem),
        //树元素容器
        $tree = $container.find('.dropdown-menu > div').attr('id', elem.data('jstreeloaderTarget'));

      $tree.jstree({
        core: {
          data: [store],
          themes: {
            responsive: false
          }
        },
        "types" : {
			"default" : {
				"icon" : "fa fa-folder icon-state-warning icon-lg"
			},
			"file" : {
				"icon" : "fa fa-file icon-state-warning icon-lg"
			}
		},
        plugins: ['types']
      });

      //
      //节点加载完成时设置默认选中值
      //
      $tree.on('ready.jstree', function(e, data) {
        var val = elem.val();
        if (val) {
          data.instance.select_node(val);
        }
        elem.data('jstree', data.instance);
      });

      //
      //节点选中时设置下拉框显示值
      //
      $tree.on('changed.jstree', function(e, data) {
    	if(data.node){
	        elem.click().val(data.node.id);
	        $container.find('.filter-option').html(data.node.text);
    	}
      });

      elem.addClass('jstreeselect-hidden-input');
    }
  };

  $.fn.jstreeselect = function(options) {
    return this.each(function() {
      render($(this), options);
    });
  };

  $.fn.jstreeVal = function(val) {
    $(this).data('jstree').select_node(val);
    return $(this).val(val);
  };

} (window.jQuery, window.app);

/**
 * 下拉树控件(多选)加载器
 */
+ function($, app) {
  var container =
    '<div class="btn-group jstreeselect form-control">' +
    '  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">' +
    '    <span class="filter-option pull-left">&nbsp;</span>' +
    '    <span class="caret"></span>' +
    '  </button>' +
    '  <div class="dropdown-menu hold-on-click">' +
    '    <div class="jstree"></div>' +
    '  </div>' +
    '</div>';

  var render = function(elem, options) {
    //
    //获取数据源
    //
    var store = elem.data('jstreemultiselectStore');
    if (store) {
      store = options[store];
    }

    if (store) {

      var
        //dropdown容器
        $container = $(container).insertAfter(elem),
        //树元素容器
        $tree = $container.find('.dropdown-menu > div.jstree').attr('id', elem.data('jstreeloaderTarget'));

      $tree.jstree({
        core: {
          data: [store],
          themes: {
            responsive: false
          }
        },
        types: {
          default: {
            icon: 'fa fa-folder icon-state-warning icon-lg'
          },
          file: {
            icon: 'fa fa-file icon-state-warning icon-lg'
          }
        },
        plugins: ['checkbox','types']
      });

      //
      //节点加载完成时设置默认选中值
      //
      $tree.on('ready.jstree', function(e, data) {
        var val = elem.val();
        if (val) {
          var vals = val.split(',');
          $.each(vals, function(i, v){
            data.instance.select_node(v);
          });
        }
        elem.data('jstree', data.instance);
      });

      //
      //节点选中时设置下拉框显示值
      //
      $tree.on('changed.jstree', function(e, data) {
        var ids = [];
        var names = [];
        var instance = elem.data('jstree');
        $.each(instance.get_selected(), function(e, id) {
          var node = instance.get_node(id);
          var type = node.li_attr['type'];
          if(elem.data('keyType')) {
            if(type == elem.data('keyType')) {
              ids.push(id);
              names.push(node.text);
            }
          }else{
            ids.push(id);
            names.push(node.text);
          }
        });
        elem.val(ids.toString());
        $(elem.data('textInput')).val(names.toString());
        $container.find('.filter-option').html(names.toString());
        if(names.length == 0) {
          $container.find('.filter-option').html('请选择...');
        }
      });

      elem.addClass('jstreemultiselect-hidden-input');
    }
  };

  $.fn.jstreemultiselect = function(options) {
    return this.each(function() {
      render($(this), options);
    });
  };

  $.fn.jstreemultiVal = function(val) {
    var thiz = $(this);
    thiz.data('jstree').deselect_all();
    if(val && val != ''){
      var vals = val.split(',');
      $.each(vals, function(i, v) {
        thiz.data('jstree').select_node(v);
      });
    }
    return thiz.val(val);
  };

} (window.jQuery, window.app);
